package cn.gudian.cms.News

class ArcTypeController {


  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [arcTypeInstanceList: ArcType.findAll("from ArcType  where parent=null")]
  }

  def create = {
    def arcTypeInstance = new ArcType()
    arcTypeInstance.properties = params
    return [arcTypeInstance: arcTypeInstance]
  }

  def save = {
    //取得最大排序值
    def orderNum = ArcType.executeQuery("select max(orderNum) from ArcType");
    if(orderNum[0]!=null)
          orderNum=orderNum[0]+1
    else
         orderNum=0
    def arcTypeInstance = new ArcType(params)
    arcTypeInstance.orderNum = orderNum
    if (!arcTypeInstance.hasErrors() && arcTypeInstance.save()) {
      flash.message = "arcType.created"
      flash.args = [arcTypeInstance.id]
      flash.defaultMessage = "ArcType ${arcTypeInstance.id} created"
      redirect(action: "list", id: arcTypeInstance.id)
    }
    else {
      render(view: "create", model: [arcTypeInstance: arcTypeInstance])
    }
  }

  def show = {
    def arcTypeInstance = ArcType.get(params.id)
    if (!arcTypeInstance) {
      flash.message = "arcType.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "ArcType not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [arcTypeInstance: arcTypeInstance]
    }
  }

  def edit = {
    def arcTypeInstance = ArcType.get(params.id)
    if (!arcTypeInstance) {
      flash.message = "arcType.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "ArcType not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [arcTypeInstance: arcTypeInstance]
    }
  }

  def update = {
    def arcTypeInstance = ArcType.get(params.id)
    if (arcTypeInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (arcTypeInstance.version > version) {

          arcTypeInstance.errors.rejectValue("version", "arcType.optimistic.locking.failure", "Another user has updated this ArcType while you were editing")
          render(view: "edit", model: [arcTypeInstance: arcTypeInstance])
          return
        }
      }
      arcTypeInstance.properties = params
      if (!arcTypeInstance.hasErrors() && arcTypeInstance.save()) {
        flash.message = "arcType.updated"
        flash.args = [params.id]
        flash.defaultMessage = "ArcType ${params.id} updated"
        redirect(action: "show", id: arcTypeInstance.id)
      }
      else {
        render(view: "edit", model: [arcTypeInstance: arcTypeInstance])
      }
    }
    else {
      flash.message = "arcType.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "ArcType not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def arcTypeInstance = ArcType.get(params.id)
    if (arcTypeInstance) {
      try {
        arcTypeInstance.delete()
        flash.message = "arcType.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "ArcType ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "arcType.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "ArcType ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "arcType.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "ArcType not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        ArcType.executeUpdate("delete ArcType where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }

  }

  def move = {
    def arcTypeInstance = ArcType.get(params.id)
    if (!arcTypeInstance) {
      flash.message = "arcType.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "ArcType not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [arcTypeInstance: arcTypeInstance]
    }
  }

  def moveSave = {

    def arcTypeInstance = ArcType.get(params.id)
    println "move" + arcTypeInstance
    if (arcTypeInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (arcTypeInstance.version > version) {

          arcTypeInstance.errors.rejectValue("version", "arcType.optimistic.locking.failure", "Another user has updated this ArcType while you were editing")
          render(view: "edit", model: [arcTypeInstance: arcTypeInstance])
          return
        }
      }
      arcTypeInstance.properties = params
      if (!arcTypeInstance.hasErrors() && arcTypeInstance.save()) {
        flash.message = "arcType.updated"
        flash.args = [params.id]
        flash.defaultMessage = "ArcType ${params.id} updated"
        redirect(action: "show", id: arcTypeInstance.id)
      }
      else {
        render(view: "edit", model: [arcTypeInstance: arcTypeInstance])
      }
    }
    else {
      flash.message = "arcType.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "ArcType not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }
}
